package com.huawei.app.demo;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.app.demo.util.LogUtils;

/**
 * 消息处理, 用于更新UI控件
 *
 * @since 2021-01-13
 */
public class MsgHandler extends Handler {

    private static final String TAG = MsgHandler.class.getSimpleName();

    private WeakReference<Activity> activityRef;
    public static final int UPDATE_DISPLAY_MODE = 20001;
    public static final int UPDATE_VIEW_TEXT = 20002;
    public static final int UPDATE_VIEW_CHECK_STATUS = 20003;
    public static final int SHOW_TOAST = 20004;
    public static final int UPDATE_FACE_DETECTION_RESULT = 20005;
    public static final int CLEAR_FACE_DETECTION_RESULT = 20006;
    public static final int UPDATE_VIEW_SHOW_OR_HIDE = 20007;
    public static final int UPDATE_EDIT_TEXT_VALUE = 20008;

    public MsgHandler(Activity mainActivity) {
        this.activityRef = new WeakReference<>(mainActivity);
    }

    @Override
    public void handleMessage(Message msg) {
        if (activityRef.get() == null) {
            return;
        }

        if (msg.what == UPDATE_DISPLAY_MODE) {
            if (!(msg.obj instanceof WindowManager.LayoutParams)) {
                return;
            }

            WindowManager.LayoutParams params = (WindowManager.LayoutParams)msg.obj;
            activityRef.get().getWindow().setAttributes(params);
            LogUtils.getInstance().i(TAG, "set Display.Mode " + msg.arg1 + " x " + msg.arg2);
        } else if (msg.what == UPDATE_VIEW_TEXT) {
            UpdateViewText msgInfo = (UpdateViewText)msg.obj;
            TextView view = activityRef.get().findViewById(msgInfo.getId());
            view.setText(msgInfo.getText());
        } else if (msg.what == UPDATE_VIEW_CHECK_STATUS) {
            UpdateViewCheckStatus msgInfo = (UpdateViewCheckStatus)msg.obj;
            CheckBox view = activityRef.get().findViewById(msgInfo.getId());
            view.setChecked(msgInfo.isChecked());
            if (msgInfo.isSendClickEvent()) {
                view.callOnClick();
            }
        } else if (msg.what == SHOW_TOAST) {
            Toast.makeText(activityRef.get(), String.valueOf(msg.obj), msg.arg1).show();
        } else if (msg.what == UPDATE_FACE_DETECTION_RESULT) {
            // 更新人脸检测结果
            ImageView imgView = activityRef.get().findViewById(R.id.faceImg);
            if (imgView == null) {
                LinearLayout topRightPanel = activityRef.get().findViewById(R.id.topRightPanel);
                if (topRightPanel.getChildCount() > 0) {
                    topRightPanel.removeAllViews();
                }
                LayoutInflater inflater = activityRef.get().getLayoutInflater();
                View view = inflater.inflate(R.layout.face_detection_view, null);
                imgView = view.findViewById(R.id.faceImg);
                topRightPanel.addView(view);
            }

            byte[] imgData = (byte[])msg.obj;
            Bitmap faceBitMap = BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
            imgView.setImageBitmap(faceBitMap);
        } else if (msg.what == CLEAR_FACE_DETECTION_RESULT) {
            // 清空人脸检测结果
            ImageView imgView = activityRef.get().findViewById(R.id.faceImg);
            if (imgView != null) {
                LinearLayout topRightPanel = activityRef.get().findViewById(R.id.topRightPanel);
                if (topRightPanel.getChildCount() > 0) {
                    topRightPanel.removeAllViews();
                }
            }
        } else if (msg.what == UPDATE_VIEW_SHOW_OR_HIDE) {
            int viewId = msg.arg1;
            View view = activityRef.get().findViewById(viewId);
            view.setVisibility((int)msg.obj);
        } else if (msg.what == UPDATE_EDIT_TEXT_VALUE) {
            int viewId = msg.arg1;
            EditText view = activityRef.get().findViewById(viewId);
            view.setText(String.valueOf(msg.obj));
        }
    }

    public static class UpdateViewText {
        private int id;
        private String text;
        public UpdateViewText(int id, String text) {
            this.id = id;
            this.text = text;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }

    public static class UpdateViewCheckStatus {
        private int id;
        private boolean checked;
        private boolean sendClickEvent;
        public UpdateViewCheckStatus(int id, boolean checked, boolean sendClickEvent) {
            this.id = id;
            this.checked = checked;
            this.sendClickEvent = sendClickEvent;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public boolean isSendClickEvent() {
            return sendClickEvent;
        }

        public void setSendClickEvent(boolean sendClickEvent) {
            this.sendClickEvent = sendClickEvent;
        }
    }
}
